/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package metier;

import java.io.File;
import java.io.FileOutputStream;
import javax.xml.stream.XMLStreamException;
import org.xml.sax.SAXException;
import org.xml.sax.SAXParseException;
import org.xml.sax.helpers.DefaultHandler;
import java.util.List;
import javax.xml.stream.XMLStreamReader;
import javax.xml.stream.events.XMLEvent;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.StringWriter;
import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;
import javax.xml.stream.XMLInputFactory;
import javax.xml.stream.XMLOutputFactory;
import javax.xml.stream.XMLStreamWriter;

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
/**
 *
 * @author charly
 */
public class GestionXml {

    protected static class ErrorHandler extends DefaultHandler {

        public void error(SAXParseException parseException) throws SAXException {
            printException(parseException);
        }

        public void fatalError(SAXParseException parseException) throws SAXException {
            printException(parseException);
        }

        public void warning(org.xml.sax.SAXParseException parseException)
                throws org.xml.sax.SAXException {
            printException(parseException);
        }

        private void printException(SAXParseException exception) {
            System.out.println(exception);
            System.out.println("line/column: " + exception.getLineNumber() + "/" + exception.getColumnNumber());
        }
    }

    /**
     * Allow matching between the file and a keyword.
     * @param FichierXML
     * @param motcle
     * @return
     * @throws Exception
     *
     * 
     */
    public String rechercherMotsCle(String fichierXML, List<String> motscle) throws Exception {
        XMLInputFactory xmlif = XMLInputFactory.newInstance();

        XMLStreamReader xmlr = xmlif.createXMLStreamReader(new FileReader(fichierXML));
        String titre = "null";
        int eventType;
        while (xmlr.hasNext()) {
            eventType = xmlr.next();
            if (eventType == XMLEvent.START_ELEMENT) {
                if (xmlr.getName().toString().equals("titre")) {
                    titre = xmlr.getElementText();
                } else if (xmlr.getName().toString().equals("motcle")) {
                    String donnee = xmlr.getElementText();
                    for (int i = 0; i < motscle.size(); i++) {
                        if (donnee.equals(motscle.get(i))) {
                            return titre;
                        }
                    }
                }
            }
        }
        return "null";
    }

    public Map<Integer, String> deserialisation(String idFileName) throws Exception {
        Map<Integer, String> results = new HashMap<Integer, String>();

        XMLInputFactory xmlif = XMLInputFactory.newInstance();

        XMLStreamReader xmlr = xmlif.createXMLStreamReader(new FileReader(idFileName));
        String titre = "null";
        int eventType;
        while (xmlr.hasNext()) {
            eventType = xmlr.next();
            if (eventType == XMLEvent.START_ELEMENT) {
                if (xmlr.getName().toString().equals("entree")) {
                    int id = Integer.parseInt(xmlr.getAttributeValue(0));
                    String fileName = xmlr.getElementText();
                    results.put(id, fileName);
                }
            }
        }
        return results;
    }

    public void serialisation(String fileName, Map<Integer, String> index) throws Exception {
        String ns = "http://www.doc.org";

        System.out.println("Serialisation");
        FileWriter output = new FileWriter(new File(fileName));

        XMLOutputFactory xmlor = XMLOutputFactory.newInstance();
        XMLStreamWriter writer = xmlor.createXMLStreamWriter(output);

        writer.writeStartDocument();
        writer.setPrefix("doc", ns);
        writer.setDefaultNamespace(ns);
        writer.writeStartElement("doc", "index", ns);
        writer.writeNamespace("doc", ns);
        writer.writeNamespace("xsi", "http://www.w3.org/2001/XMLSchema-instance");
        writer.writeAttribute("xsi:schemaLocation", ns + " index.xsd");

        for (Entry<Integer, String> entry : index.entrySet()) {
            writer.writeStartElement("entree");
            writer.writeAttribute("id", "" + entry.getKey());
            writer.writeCharacters(entry.getValue());
            writer.writeEndElement();
        }

        writer.writeEndElement();
        writer.flush();
        writer.close();
    }
}